/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ui;

import com.michaelbaranov.microba.calendar.DatePicker;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractCellEditor;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Esteban
 */
public class DatePickerCell extends AbstractCellEditor implements TableCellEditor, TableCellRenderer{
    DatePicker picker;
    
    public DatePickerCell() {
        picker = new com.michaelbaranov.microba.calendar.DatePicker();
        picker.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
        picker.setFieldEditable(false);
        picker.setEnabled(true);
        /*try{
            picker.setDate(Calendar.getInstance().getTime());
        }catch(PropertyVetoException f){}*/
        picker.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                pickerActionPerformed(evt);
            }
        });
    }
    
    public DatePicker getDatePicker(){
        return picker;
    }
    public void setDatePicker(DatePicker pickerRef){
        if(pickerRef==null){
            System.out.print("ohhhh");
        }
        if(pickerRef.getDateFormat().getCalendar().getTime()!=null){
            picker=pickerRef;
        }
    }
    
    private void pickerActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        try {
            // TODO add your handling code here:
            System.out.print("hizo algo");
            picker.setDate(picker.getDateFormat().getCalendar().getTime());
        } catch (PropertyVetoException ex) {
            Logger.getLogger(DatePickerCell.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {
        if(isSelected){
            System.out.print("acaa");
                try {
                    picker.setDate(null);
                } catch (PropertyVetoException ex) {
                    Logger.getLogger(DatePickerCell.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
        return picker;
    }

 
    @Override
    public Object getCellEditorValue() {
        return null;
    }
 
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
        return picker;
    }
}
